package com.example.controller;

import com.example.common.RequirePermission;
import com.example.common.Result;
import com.example.dto.MenuDTO;
import com.example.dto.MenuPermissionDTO;
import com.example.service.MenuManagementService;
import com.example.service.MenuPermissionService;
import com.example.vo.MenuManagementVO;
import com.example.vo.MenuDocumentationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admin/menu-management")
@CrossOrigin
public class MenuManagementController {
    
    @Autowired
    private MenuManagementService menuManagementService;
    
    @Autowired
    private MenuPermissionService menuPermissionService;
    
    /**
     * 获取所有菜单（树形结构）
     */
    @GetMapping("/tree")
    @RequirePermission("admin:menu:list")
    public Result<List<MenuManagementVO>> getAllMenusTree() {
        return menuManagementService.getAllMenusTree();
    }
    
    /**
     * 获取所有菜单（平铺结构）
     */
    @GetMapping("/list")
    @RequirePermission("admin:menu:list")
    public Result<List<MenuManagementVO>> getAllMenusList() {
        return menuManagementService.getAllMenusList();
    }
    
    /**
     * 根据ID获取菜单详情
     */
    @GetMapping("/{id}")
    @RequirePermission("admin:menu:view")
    public Result<MenuManagementVO> getMenuById(@PathVariable Long id) {
        return menuManagementService.getMenuById(id);
    }
    
    /**
     * 新增菜单
     */
    @PostMapping
    @RequirePermission("admin:menu:add")
    public Result<Void> addMenu(@RequestBody MenuDTO menuDTO) {
        return menuManagementService.addMenu(menuDTO);
    }
    
    /**
     * 更新菜单
     */
    @PutMapping("/{id}")
    @RequirePermission("admin:menu:update")
    public Result<Void> updateMenu(@PathVariable Long id, @RequestBody MenuDTO menuDTO) {
        return menuManagementService.updateMenu(id, menuDTO);
    }
    
    /**
     * 删除菜单
     */
    @DeleteMapping("/{id}")
    @RequirePermission("admin:menu:delete")
    public Result<Void> deleteMenu(@PathVariable Long id) {
        return menuManagementService.deleteMenu(id);
    }
    
    /**
     * 获取父级菜单选项（用于下拉选择）
     */
    @GetMapping("/parent-options")
    @RequirePermission("admin:menu:list")
    public Result<List<MenuManagementVO>> getParentMenuOptions() {
        return menuManagementService.getParentMenuOptions();
    }
    
    /**
     * 获取菜单说明文档
     */
    @GetMapping("/documentation")
    @RequirePermission("admin:menu:view")
    public Result<MenuDocumentationVO> getMenuDocumentation() {
        return menuManagementService.getMenuDocumentation();
    }
    
    /**
     * 为菜单绑定权限
     */
    @PostMapping("/{id}/permissions")
    @RequirePermission("admin:menu:permission:bind")
    public Result<String> bindPermissionsToMenu(@PathVariable Long id, @RequestBody MenuPermissionDTO dto) {
        dto.setMenuId(id);
        return menuPermissionService.bindPermissionsToMenu(dto);
    }
    
    /**
     * 获取菜单绑定的权限ID列表
     */
    @GetMapping("/{id}/permissions")
    @RequirePermission("admin:menu:permission:view")
    public Result<List<Long>> getMenuPermissions(@PathVariable Long id) {
        return menuPermissionService.getMenuPermissions(id);
    }
    
    /**
     * 删除菜单的权限绑定
     */
    @DeleteMapping("/{id}/permissions")
    @RequirePermission("admin:menu:permission:delete")
    public Result<String> removeMenuPermissions(@PathVariable Long id) {
        return menuPermissionService.removeMenuPermissions(id);
    }
    
    /**
     * 同步菜单权限到相关角色
     */
    @PostMapping("/{id}/permissions/sync")
    @RequirePermission("admin:menu:permission:sync")
    public Result<String> syncMenuPermissionsToRoles(@PathVariable Long id) {
        return menuPermissionService.syncRolePermissions(id);
    }
} 